<?php
declare (strict_types=1);

namespace app\admin\controller;

use think\App;
use think\exception\ValidateException;
use think\facade\Console;
use think\facade\View;
use app\common\model\Menu as MenuModel;
use think\response\Json;

class Menu extends Base
{
    protected object $model;

    protected string $pk = 'id';

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new MenuModel();
    }

    /**
     * 原点 2020年8月5日14:25:13 首页
     *
     * @return string
     */
    public function index(): string
    {

        View::assign('menus', $this->model->getMenu('', 2));
        return View::fetch();

    }


    /**
     * 原点 2020年8月5日15:50:01 新增和编辑菜单
     *
     * @param $id -- 编辑的菜单ID
     * @param string $pid -- 新增子菜单的父级菜单ID
     * @return string
     */
    public function edit($name = '', $id = '', $pid = '', $uid = '')
    {
        try {
            $p_title = '';
            if ($pid) {
                $p_title = $this->model->where('id', $pid)->value('title');
            }
            View::assign('p_title', $p_title);
            View::assign('info', $this->model->find($id));
            return View::fetch();

        } catch (\Exception $e) {
            return $e->getMessage();
        }

    }

    /**
     * 原点 2020年8月5日16:05:02 保存新增的菜单
     */
    public function save1(): Json
    {
        $param = $this->request->post();
        try {

            parent::validate($param, 'menu');

        } catch (ValidateException $e) {

            return json(['code' => -1, 'msg' => $e->getError()]);
        }

//        $this->request->logs = $param['id'] ? '编辑菜单' : '新增菜单';
        if ($param['id']) {

            $this->model::update($param);
            return json(['code' => 1, 'msg' => '操作成功']);
        } else {

            try {
                //自动构建控制器
                if (isset($param['autoController']) && $param['autoController'] == '1') {
                    if (!$param['className'] || !$param['classNamespace']) {
                        return json(['code' => -1, 'msg' => '已开启自动构建控制器，请填写控制器相关参数']);
                    }
                    Console::call('_controller', [$param['classNamespace'] . $param['className']]);

                }
                //自动构建模型
                if (isset($param['autoModel']) && $param['autoModel'] == '1') {
                    if (!$param['modelName'] || !$param['modelNamespace'] || !$param['dbName']) {
                        return json(['code' => -1, 'msg' => '已开启自动构建模型，请填写模型相关参数']);
                    }

                    Console::call('_model', [$param['modelNamespace'] . $param['modelName'] . ',' . $param['dbName']]);
                }

                $this->model::create($param);
                return json(['code' => 1, 'msg' => '操作成功']);
            } catch (\Exception $e) {
                return json(['code' => -1, 'msg' => $e->getMessage()]);
            }

        }

//        return $this->model->add($param);
    }

    /**
     * 原点 2020年8月4日10:38:36 更新状态
     */
    public function updateStatus($key, $val, $id): Json
    {
        try {
            if ($val == 'true') $val = 1;
            if ($val == 'false') $val = 0;
            $this->model::update([$key => $val], ['id' => $id]);
            return json(['code' => 1, 'msg' => '操作成功']);

        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }

    }
}
